package com.crowdgeeks.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.w3c.dom.Document;

import com.google.code.facebookapi.FacebookWebappHelper;
import com.google.code.facebookapi.FacebookXmlRestClient;
import com.google.code.facebookapi.IFacebookRestClient;
import com.sun.xml.internal.ws.util.StringUtils;

public class LoginCheckInterceptor extends HandlerInterceptorAdapter {
	
	private String apiKey;
	private String appSecretCode;

	public void setApiKey(String apiKey) {
		this.apiKey = apiKey;
	}

	public void setAppSecretCode(String appSecretCode) {
		this.appSecretCode = appSecretCode;
	}
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		
		
//		System.out.println("*** apiKey : " +apiKey);
//		System.out.println("*** appSecretCode : " +appSecretCode);
		
		IFacebookRestClient<Document> userClient = new FacebookXmlRestClient(this.apiKey, this.appSecretCode);
		
		FacebookWebappHelper<Document> webappHelper = new FacebookWebappHelper<Document>(request, response, this.apiKey, this.appSecretCode, userClient);
		
//		HttpSession session = request.getSession(true);
		System.out.println("*** session : " +org.apache.commons.lang.StringUtils.defaultIfEmpty(request.getParameter("session"), "12345"));
//		System.out.println("*** session : " +session.getAttribute("session_key"));
//		System.out.println("*** session : " +session.getAttribute("sig"));
		
//        IFacebookRestClient<Document> userClient = (FacebookXmlRestClient)session.getAttribute("facebook.user.client");
//        IFacebookRestClient<Document> userClient = new FacebookXmlRestClient(apiKey, appSecretCode);
//        userClient.
        
//        System.out.println("*** userClient : " +userClient);
//        System.out.println("*** userClient : " +userClient.auth_createToken());
        System.out.println("**************************************");
        
//        if (userClient == null) {
////            logger.debug("User session doesn't have a Facebook API client setup yet. Creating one and storing it in the user's session.");
//            userClient = new FacebookXmlRestClient(this.apiKey, this.appSecretCode);
//            session.setAttribute("facebook.user.client", userClient);
//            
//			FacebookWebappHelper<Document> webappHelper = new FacebookWebappHelper<Document>(request, response, this.apiKey, this.appSecretCode, userClient);
//			webappHelper.requireLogin("");
//            return false;
//            
//        } else {
//        	return true;
//        }
//        return false;
		
		System.out.println("*** webappHelper.isLogin() : " +webappHelper.isLogin());
		
		if (webappHelper.isLogin()) {
			
			return true;
			
		} else {
			
			webappHelper.requireLogin("");
			
			return false;
		}
	}

}
